package org.sdp.pearproject.service.impl;


import org.sdp.pearproject.dto.convert.*;
import org.sdp.pearproject.dao.domain.*;
import org.sdp.pearproject.dto.request.*;
import org.sdp.pearproject.dto.response.*;
import org.sdp.pearproject.dao.mapper.*;
import org.sdp.pearproject.service.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: MemberServiceSuperImpl
 * @Description:
 * @Author: SDP
 * @Date: 2022-07-18
 * @Version: 1.0
 * Table: pear_member
 * Comment:
 * 用户表
 */
public class MemberServiceSuperImpl extends BaseServiceImpl implements MemberService {
    @Autowired
    protected MemberMapper memberMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        map.put("code", "编号");
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addMember(MemberAddRequest request) throws Exception {
        MemberWithBLOBs daoRequest = new MemberAddRequestConvert().convert(request);
        fixEqualCondition(request, "member", "add", "", null);
        Integer result = 0;
        try {
            result = memberMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            result = daoRequest.getId();
        }
        addHistory(request, "A", "member", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateMember(MemberUpdateRequest request) throws Exception {
        fixEqualCondition(request, "member", "update", request.getId()+"", null);
        checkInternalId(request, "member", "update", request.getId());
        addHistory(request, "U", "member", "api", request.getId(), null);

        MemberWithBLOBs daoRequest = new MemberUpdateRequestConvert().convert(request);
        MemberExample example = new MemberExample();
        example.createCriteria().andIdEqualTo(request.getId());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), MemberExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = memberMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteMember(MemberDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "member", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "member", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "member", "api", request.getIdList().get(index), null);
            memberMapper.deleteByPrimaryKey(request.getIdList().get(index));
            count++;
        }
        return count;
    }

    @Override
    public MemberQueryResponse queryMember(MemberQueryRequest request) throws Exception {
        MemberWithBLOBs daoResponse = memberMapper.selectByPrimaryKeyWithBLOBs(request.getId());
        MemberQueryResponse response = new MemberQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<MemberQueryResponse> listMember(MemberQueryRequest request) throws Exception {
        MemberWithBLOBs daoRequest = new MemberWithBLOBs();
        copyProperties(request, daoRequest);
        MemberExample example;
        try {
            example = MemberExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<MemberWithBLOBs> list;
        try {
            list = memberMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<MemberQueryResponse> responseList = new ArrayList<>();
        for(MemberWithBLOBs item : list) {
            MemberQueryResponse newItem = new MemberQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countMember(MemberQueryRequest request) throws Exception {
        MemberWithBLOBs daoRequest = new MemberWithBLOBs();
        copyProperties(request, daoRequest);
        MemberExample example;
        try {
            example = MemberExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = memberMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statMember(MemberQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
